package com.yonyou.brigade.rtpnr.util;

import com.yonyou.ybzboot.starter.common.exception.BaseException;
import com.yonyou.ybzboot.starter.common.exception.BaseException;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;

/**
 * Created by Administrator on 2018/6/11.
 */
public class CXFInvoke {

    private static  Logger logger= LoggerFactory.getLogger(CXFInvoke.class);

    public static Object invoke(String url,String method,Object... args){
        // 创建动态客户端
        String uuid = UUID.randomUUID().toString();
        logger.info("请求地址("+uuid+"):"+url);
        logger.info("请求参数("+uuid+"):"+args.toString());
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(url);
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,
        // PASS_WORD));
        Object[] objects = null;
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke(method, args);
            System.out.println("返回数据:" + objects[0]);
            logger.info("返回结果("+uuid+"):"+objects[0]);
        } catch (java.lang.Exception e) {
            String errorMsg = "请求中航易购接口出错("+uuid+")";
            logger.error(errorMsg + ":" + e.getMessage());
            throw new BaseException(-100,errorMsg);
        }
        return objects[0];
    }
}
